package jinyilw.swing.visuals.button;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.plaf.synth.SynthButtonUI;

import jinyilw.swing.ComponentTools;

public class CButton extends JButton
{
	private static final long serialVersionUID = 6232247522890196955L;
	private static final AlphaComposite alphaComposite = ComponentTools.alphaComposite;
	private float alpha = 1f;

	public CButton()
	{
	}

	public CButton(Icon icon)
	{
		super(icon);
	}

	public CButton(String text)
	{
		super(text);
	}

	public CButton(Action a)
	{
		super(a);
	}

	public CButton(String text, Icon icon)
	{
		super(text, icon);
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g.create();
		Composite composite = g2d.getComposite();
		float al = isEnabled() ? alpha : 0.3f;
		g2d.setComposite(alphaComposite.derive(al));
		super.paintComponent(g2d);
		g2d.setComposite(composite);
	}

	@Override
	public void updateUI()
	{
		setUI(new SynthButtonUI());
	}
}
